package com.example.demo.controller;

import com.example.demo.config.ResponseModel;
import com.example.demo.entity.Menu;
import com.example.demo.exception.BusinessException;
import com.example.demo.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@RequestMapping("/menu")
@Api(value = "菜单controller",tags = "菜单操作接口")
public class MenuController extends BaseController{
    @Autowired
    private MenuService menuService;

    @GetMapping("/findById/{id}")
    @ApiOperation(value="根据id查询菜品",tags={"菜品增删改查"},httpMethod = "GET")
    @ApiImplicitParam(name = "id",value = "菜品id",dataType = "Long",paramType = "query")
    public ResponseModel findById(@PathVariable("id") Long id){
        return new ResponseModel(menuService.findById(id),"sucess","200");
    }


    /**
     * @yucs 这是纯后端的，用于做测试，具体展示还是看queryMenuList吧
     * 和queryUserList类似
     * @return
     */
    @GetMapping("/findAllMenu")
    @ApiOperation(value="查询所有菜品",httpMethod = "GET")
    public List<Menu> findAllMenu(){
        return menuService.findAllMenu();
    }


    /**
     * @yucs 2021/8/26
     * 将所有的菜品直接返回展示
     * TODO：分页显示
     * @return
     */
    @GetMapping("/queryMenuList")
    @ResponseBody
    @ApiOperation(value="查询所有菜品",tags={"菜品增删改查"},httpMethod = "GET")
    @Cacheable(cacheNames = "menuList", key = "'queryMenuList'")
    @ApiImplicitParams({@ApiImplicitParam(name = "page",value = "分页数",dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(name = "limit",value = "行数",dataType = "Integer",paramType = "query")})
    public ResponseModel queryMenuList(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit){
        List<Menu> menuList = menuService.queryMenuList(page,limit);
        return new ResponseModel(menuList,"","200");
    }

    @GetMapping("/findCatalogById/{id}")
    @ApiOperation(value="查找菜品类别",httpMethod = "GET")
    @ApiImplicitParam(name = "id",value = "菜品id",dataType = "Long",paramType = "query")
    public List<?> findCatalogById(@PathVariable("id") Long id){
        return menuService.findCatalogById(id);
    }

    /**
     * 用于测试MongoDB的代码
     * @param collection
     */
    @PostMapping("/createCollection/{collection}")
    public void createCollection(@PathVariable("collection") String collection){
        menuService.CreateCollection(collection);
    }

    /**
     * @yucs 2021/8/24
     * 根据ID删除菜品（物理删除）
     */
    @RequestMapping(value = "/admin/delete/{Id}", method = RequestMethod.POST)
    @ApiOperation(value="根据id删除菜品（物理删除）",httpMethod = "POST")
    @ApiImplicitParam(name = "Id",value = "菜品id",dataType = "Long",paramType = "query")
    public ResponseModel<Object> delteById(HttpSession session, @PathVariable("Id") Long menuId) throws BusinessException {
        boolean delSuccess = menuService.deleteById(session,menuId);
        if(delSuccess){
            return new ResponseModel<>(null,"success","200");
        }else{
            //状态码和HTTP状态码保持一致
            return new ResponseModel<>(null,"failed","500");
        }
    }

    /**
     * @yucs 2021/8/24
     * 根据ID下架菜品（逻辑删除）
     */
    @RequiresRoles("admin")
    @RequestMapping(value = "/admin/offLoad/{Id}", method = RequestMethod.POST)
    @ApiOperation(value="根据id删除菜品（逻辑删除）",httpMethod = "POST")
    @ApiImplicitParam(name = "Id",value = "菜品id",dataType = "Long",paramType = "query")
    public ResponseModel<Object> offLoadById(HttpSession session, @PathVariable("Id") Long menuId) throws BusinessException {
//        boolean delSuccess = false;
        boolean offLoadSuccess = menuService.offLoadById(session,menuId);
        if(offLoadSuccess){
            return new ResponseModel<>(null,"success","200");
        }else{
            //状态码和HTTP状态码保持一致
            return new ResponseModel<>(null,"failed","500");
        }
    }

    @RequiresRoles("admin")
    @RequestMapping(value = "/admin/upLoad/{Id}", method = RequestMethod.POST)
    @ApiImplicitParam(name = "Id",value = "菜品id",dataType = "Long",paramType = "query")
    public ResponseModel<Object> upLoadById(HttpSession session, @PathVariable("Id") Long menuId) throws BusinessException {
//        boolean delSuccess = false;
        boolean offLoadSuccess = menuService.upLoadById(session,menuId);
        if(offLoadSuccess){
            return new ResponseModel<>(null,"success","200");
        }else{
            //状态码和HTTP状态码保持一致
            return new ResponseModel<>(null,"failed","500");
        }
    }

    //修改菜品
    @PostMapping("/admin/updateMenu")
    @ApiOperation(value="修改菜品",tags={"菜品增删改查"},httpMethod = "POST")
    public ResponseModel updateMenu(Menu menu){
        Boolean flag = menuService.updateMenu(menu)>0;
        if(flag){
            return new ResponseModel<>(null,"success","200");
        }else{
            //状态码和HTTP状态码保持一致
            return new ResponseModel<>(null,"failed","500");
        }
    }

    //删除菜品
    @DeleteMapping("/admin/deleteMenu/{id}")
    @ApiOperation(value="删除菜品",tags={"菜品增删改查"},httpMethod = "DELETE")
    @ApiImplicitParam(name = "id",value = "菜品id",dataType = "Long",paramType = "query")
    public ResponseModel deleteMenu(@PathVariable("id") Long id){
        Boolean flag = menuService.deleteMenu(id)>0;
        if(flag){
            return new ResponseModel<>(null,"success","200");
        }else{
            //状态码和HTTP状态码保持一致
            return new ResponseModel<>(null,"failed","500");
        }
    }

    //添加菜品
    @PostMapping("/admin/createMenu")
    @ResponseBody
    @ApiOperation(value="创建菜品",tags={"菜品增删改查"},httpMethod = "POST")
    public ResponseModel createMenu(@RequestBody Menu menu){
        Boolean flag = menuService.createMenu(menu)>0;
        if(flag){
            return new ResponseModel<>(null,"success","200");
        }else{
            //状态码和HTTP状态码保持一致
            return new ResponseModel<>(null,"failed","500");
        }
    }



}
